package com.sea.finance.service.impl;

import com.sea.finance.entity.VtFile;
import com.sea.finance.mapper.VtFileMapper;
import com.sea.finance.service.VtFileService;
import com.sea.finance.service.VtService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class VtFileServiceImpl implements VtFileService {
    @Autowired
    private VtFileMapper vtFileMapper;
    @Autowired
    private VtService vtService;

    @Override
    public List<VtFile> findByCnd(VtFile vtFile) {
        return vtFileMapper.findByCnd(vtFile);
    }

    @Override
    public int save(VtFile vtFile) {
        return vtFileMapper.insert(vtFile);
    }

    @Override
    public Integer usave(VtFile vtFile) {
        return vtFileMapper.update(vtFile);
    }



    @Override
    public VtFile findById(Integer id) {
        return vtFileMapper.findById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByIds(Integer[] ids) {
        for (Integer id : ids) {
            VtFile v = findById(id);
            if (!v.getFlag().equals(0)) {
                throw new RuntimeException("请勿处理重复数据!");
            } else {
                VtFile vtFile = new VtFile();
                vtFile.setId(id);
                vtFile.setFlag(1);
                usave(vtFile);
            }
        }
    }

    @Override
    public void dealImportSku() {
        VtFile vtFile = new VtFile();
        vtFile.setFlag(1);
        List<VtFile> list = findByCnd(vtFile);
        if (null != list && list.size() != 0) {
            for (VtFile v : list) {
                //更新开始时间
                usave(v);
                //开始处理文件
                //String remark = "";
                String remark = vtService.dealFile(v.getId());
                if (StringUtils.isNotBlank(remark)) {
                    v.setRemark(remark);
                    v.setFlag(2);
                    usave(v);
                    break;
                } else {
                    v.setFlag(3);
                    v.setRemark("更新成功！");
                    usave(v);

                    //开始处理非期间数据
                    String countryName = v.getCountryName();

                    switch (countryName) {


                        case "US":
                            System.out.println(countryName);
                            vtService.deleteUSPeriod(countryName);
                            break;
                        case "CA":
                            System.out.println(countryName);
                            vtService.deletEUPeriod(countryName);
                            break;
                        case "DE":
                        case "FR":
                        case "IT":
                        case "ES":
                        case "UK":
                            System.out.println(countryName);
                            vtService.deletEUPeriod(countryName);
                            break;
                    }

                }

            }
        }
        //删除表头数据
        vtService.deleteTitle();

        vtService.updateFnSku();
        vtService.updateListingName();
    }

    @Override
    public Integer delete(Integer id) {
        VtFile vtFile = findById(id);
        vtService.delete(vtFile.getPeriod(),vtFile.getShopName(),vtFile.getBrandName(),vtFile.getCountryName());
        return vtFileMapper.delete(id);
    }
}
